<template>
  <div class="applyAudit">
    <!-- 搜索 -->
    <div class="section">
      <h3>活动申请</h3>
      <apply-search @searchList="searchData"></apply-search>
    </div>
    <el-table
      :height="tabHeight"
      border
      style="width: 99%"
      @selection-change="selectTable"
      align="center"
      :data="tableData"
    >
      <el-table-column
        type="selection"
        width="35"
        :selectable="checkboxInit"
      ></el-table-column>
      <el-table-column prop="id" label="ID" width="60"></el-table-column>
      <el-table-column prop="company_name" label="所属CP"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="game_name" label="游戏名"></el-table-column>
      <el-table-column prop="server_name" label="区服"></el-table-column>
      <el-table-column prop="role_name" label="角色名"></el-table-column>
      <el-table-column
        prop="role_id"
        label="角色ID"
        width="60"
      ></el-table-column>
      <el-table-column label="申请活动" width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.activity_name }}</span>
          <el-button
            type="text"
            size="mini"
            @click="applyEventdetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="申请时间"></el-table-column>
      <el-table-column label="申请明细" width="60">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="applydetail(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="充值记录" width="60">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="chargeRecord(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="是否包含CDK发放" width="65">
        <template slot-scope="scope">
          <span v-if="scope.row.has_cdk == 1">否</span>
          <span v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="玩家备注" width="60">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="playerRemark(scope.row)"
            v-if="scope.row.user_remark != ''"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="备注" width="50">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            size="mini"
            v-if="scope.row.audit_remark != ''"
            @click="cpRemark(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="审批状态" width="60">
        <template slot-scope="scope">
          <span v-if="scope.row.audit_status == 1">待审核</span>
          <span v-else-if="scope.row.audit_status == 2">通过</span>
          <span v-else>驳回</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100" class="operateButton">
        <template slot-scope="scope">
          <el-button
            type="success"
            plain
            size="mini"
            @click="passSuccess(scope.row)"
            v-if="scope.row.audit_status == 1"
            >通过</el-button
          >
          <el-button
            type="danger"
            plain
            size="mini"
            @click="rejectDanger(scope.row)"
            v-if="scope.row.audit_status == 1"
            >驳回</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="bottom">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="pagenationData.currentPage"
        :page-size="pagenationData.pageSize"
        :total="pagenationData.pageTotal"
        background
        layout="prev, pager, next"
        prev-text="上一页"
        next-text="下一页"
      ></el-pagination>
      <el-button type="primary" @click="passThrough">一键通过</el-button>
    </div>
    <!-- 申请明细 -->
    <ApplyDetail
      v-if="applyDetaildialog.show"
      :isShow="applyDetaildialog.show"
      :dialogRow="applyDetaildialog.dialogRow"
      @closeDialog="hideApplyDetaildialog"
    ></ApplyDetail>
    <!-- 充值记录 -->
    <ChargeRecord
      v-if="chargeRecordDialog.show"
      :isShow="chargeRecordDialog.show"
      :dialogRow="chargeRecordDialog.dialogRow"
      @closeDialog="hidechargeRecordDialog"
    ></ChargeRecord>
    <!-- 通过备注 cdk -->
    <PassRemark
      v-if="passRemarkDialog.show"
      :isShow="passRemarkDialog.show"
      :dialogRow="passRemarkDialog.dialogRow"
      :id="passRemarkDialog.id"
      @closeDialog="hidepassRemarkDialog"
    ></PassRemark>
    <!-- 驳回备注 -->
    <RejectDialog
      v-if="rejectDialog.show"
      :isShow="rejectDialog.show"
      :rowId="rejectDialog.rowId"
      @closeDialog="hiderejectDialog"
    ></RejectDialog>
    <!-- 查看详情 -->
    <EventDetail
      v-if="eventDetaildialog.show"
      :isShow="eventDetaildialog.show"
      :dialogRow="eventDetaildialog.dialogRow"
      @closeDialog="hideEventDetaildialog"
    ></EventDetail>
  </div>
</template>
<script>
import {
  getApplyList,
  operateApply,
  chargeDetail,
  onekeyPass,
  applyDetail,
  activityDetail,
  getCdkRange,
} from "@/api/eventManage";
import EventDetail from "./component/eventList/eventDetail";
import ApplySearch from "./component/applyAudit/applySearch";
import ApplyDetail from "./component/applyAudit/ApplyDetail";
import ChargeRecord from "./component/applyAudit/ChargeRecord";
import PassRemark from "./component/applyAudit/PassRemark";
import RejectDialog from "./component/applyAudit/rejectDialog";

export default {
  inject: ["reload"],
  data() {
    return {
      tabHeight: window.innerHeight - 230,
      search: {},
      tableData: [],
      loading: false,
      multipleSelection: [], //复选框选中
      eventDetaildialog: {
        show: false,
      },

      applyDetaildialog: {
        show: false,
        dialogRow: [],
      },
      chargeRecordDialog: {
        show: false,
        dialogRow: [],
      },
      passRemarkDialog: {
        show: false,
        dialogRow: {},
        id: "",
      },
      rejectDialog: {
        show: false,
        rowId: "",
      },
      pagenationData: {
        pageTotal: 0,
        currentPage: 1,
        pageSize: 20,
      },
      pageData: {
        page: 1,
        limit: 20,
      },
    };
  },
  components: {
    ApplySearch,
    ApplyDetail,
    ChargeRecord,
    PassRemark,
    RejectDialog,
    EventDetail,
  },
  mounted() {
    this.getApplyList();
  },
  methods: {
    searchData(search) {
      this.search = search;
      this.search.page = 1;
      this.pagenationData.currentPage = 1;
      this.searchList();
    },
    // 搜索数据
    searchList() {
      this.loading = true;
      const para = Object.assign({}, this.search);
      getApplyList(para).then((res) => {
        this.loading = false;
        this.pagenationData.pageTotal = res.data.count;
        this.tableData = res.data.result;
      });
    },
    // 获取数据列表
    getApplyList() {
      const para = Object.assign({}, this.pageData);
      getApplyList(para).then((res) => {
        this.loading = false;
        this.pagenationData.pageTotal = res.data.count;
        this.tableData = res.data.result;
      });
    },
    // 上下分页
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.search.page = val;
      this.searchList();
    },
    // 当用户手动勾选数据行的 Checkbox 时触发的事件
    selectTable(val, row) {
      this.multipleSelection = val;
    },
    // 查看活动详情
    applyEventdetail(row) {
      const para = Object.assign({}, { activity_id: row.activity_id });
      activityDetail(para).then((res) => {
        if (res.data == null) {
          this.eventDetaildialog.dialogRow = [];
        } else {
          this.eventDetaildialog.dialogRow = res.data;
        }
        this.eventDetaildialog.show = true;
      });
    },
    hideEventDetaildialog() {
      this.eventDetaildialog.show = false;
    },
    // 查看申请明细
    applydetail(row) {
      const para = Object.assign(
        {},
        { apply_id: row.id, activity_id: row.activity_id }
      );
      applyDetail(para).then((res) => {
        if (res.data == null) {
          this.applyDetaildialog.dialogRow = [];
        } else {
          this.applyDetaildialog.dialogRow = res.data;
        }
        this.applyDetaildialog.show = true;
      });
    },
    hideApplyDetaildialog() {
      this.applyDetaildialog.show = false;
    },
    hidechargeRecordDialog() {
      this.chargeRecordDialog.show = false;
    },
    chargeRecord(row) {
      const para = Object.assign({}, { apply_id: row.id });
      chargeDetail(para).then((res) => {
        if (res.data == null) {
          this.chargeRecordDialog.dialogRow = [];
        } else {
          this.chargeRecordDialog.dialogRow = res.data;
        }
        this.chargeRecordDialog.show = true;
      });
    },
    playerRemark(row) {
      //查看玩家备注
      this.$alert(row.user_remark, "玩家备注", {
        confirmButtonText: "确定",
        center: true,
      }).catch(() => {});
    },
    cpRemark(row) {
      //查看备注
      this.$alert(row.audit_remark, "备注", {
        confirmButtonText: "确定",
        center: true,
      }).catch(() => {});
    },
    // 通过
    passSuccess(row) {
      if (row.has_cdk == "2") {
        const para = Object.assign({}, { apply_id: row.id });
        getCdkRange(para).then((res) => {
          this.passRemarkDialog.show = true;
          this.passRemarkDialog.dialogRow = res.data.amountList;
          this.passRemarkDialog.id = row.id;
        });
      } else if (row.has_cdk == "1") {
        const para = Object.assign({}, { apply_id: row.id, status: 1 });
        operateApply(para).then((res) => {
          this.$message({
            message: "通过成功",
            type: "success",
          });
          setTimeout(() => {
            this.reload();
          }, 20);
        });
      }
    },

    hidepassRemarkDialog() {
      this.passRemarkDialog.show = false;
    },
    // 驳回
    rejectDanger(row) {
      this.rejectDialog.show = true;
      this.rejectDialog.rowId = row.id;
    },
    hiderejectDialog() {
      this.rejectDialog.show = false;
    },
    //一键通过
    passThrough() {
      let len = this.multipleSelection.length;
      if (len === 0) {
        this.$message({
          type: "warning",
          message: "请至少选择一项！",
        });
      } else {
        this.$confirm(`确定一键通过选中的 ${len} 条数据吗？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          var roIdArr = [];
          this.multipleSelection.forEach((item) => {
            roIdArr.push(item.id);
          });
          let roIdStr = roIdArr.join(",");
          onekeyPass({ apply_ids: roIdStr }).then((res) => {
            let result = res.code;
            if (result == 200) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              setTimeout(() => {
                this.reload();
              }, 20);
            } else {
              this.$message({
                type: "warning",
                message: "操作失败",
              });
            }
          });
        });
      }
    },
    // 单行checkbox禁用
    checkboxInit(row, index) {
      if (row.has_cdk == "2" || row.audit_status != 1) {
        return 0;
      } else {
        return 1;
      }
    },
  },
};
</script>
<style lang="scss">
.applyAudit {
  .section {
    justify-content: flex-start;
  }
  .linkgame {
    display: inline-block;
    width: 168px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
  }
  .cell {
    .el-button--mini,
    .el-button--mini.is-round {
      padding: 5px 6px;
    }
  }
  .el-table__row td:last-child {
    .el-button + .el-button {
      margin-left: 4px;
    }
  }
  .bottom {
    .el-button {
      padding: 0 15px;
      font-size: 13px;
    }
  }
  .el-table .cell {
    line-height: 18px;
  }
}
</style>